import request from '@/utils/request'
import type { UserInfo, UpdateUserRequest } from '@/types/user'
import type { UserListResponse } from '@/types/customer'
import type { CreateAdminDto } from '@/types/admin'
import type { ResponseData } from '@/types/common'

// 获取用户本人信息
export const getUserInfo = () => {
  return request.get<{
    code: number
    data: UserInfo
    message: string
  }>('/users/info')
}

// 更新用户本人信息
export const updateUserInfo = (data: UpdateUserRequest) => {
  return request.put<{
    code: number
    data: UserInfo
    message: string
  }>('/users/updateProfile', data)
}

// 获取客户列表
export const getCustomers = (params?: {
  keyword?: string
  status?: 'all' | 'active' | 'inactive'
  page?: number
  pageSize?: number
}) => {
  return request.get<{
    code: number
    data: UserListResponse
    message: string
  }>('/users/customers', {
    params: {
      keyword: params?.keyword || '',
      status: params?.status || 'all',
      page: params?.page || 1,
      pageSize: params?.pageSize || 10,
    },
  })
}

// 更新客户状态
export const updateCustomerStatus = (id: number) => {
  return request.patch<{
    code: number
    data: null
    message: string
  }>(`/users/${id}/status`)
}

// 更新客户信息
export const updateCustomerInfo = (id: number, data: UpdateUserRequest) => {
  return request.put<{
    code: number
    data: UserInfo
    message: string
  }>(`/users/${id}/updateInfo`, data)
}

export const getAdmins = (params?: {
  keyword?: string
  status?: 'all' | 'active' | 'inactive'
  page?: number
  pageSize?: number
}) => {
  return request.get<{
    code: number
    data: UserListResponse
    message: string
  }>('/users/admins', {
    params: {
      keyword: params?.keyword || '',
      status: params?.status || 'all',
      page: params?.page || 1,
      pageSize: params?.pageSize || 10,
    },
  })
}

// 创建管理员
export const createAdmin = (data: CreateAdminDto) => {
  return request.post<ResponseData>('/users/admin', data)
}

// 重置管理员密码
export const resetAdminPassword = (id: number) => {
  return request.post<ResponseData>(`/users/admin/${id}/reset-password`)
}
